import java.io.*;
import java.util.ArrayList;

/**
 * 算式集合基类
 *
 * @author Li Sipeng
 * @version 4.0 on   2022-11-12 22:45
 */
public class OperationBase implements Serializable{
//    private static final long serialVersionUID = 123456L;
    ArrayList<Operation> addList = new ArrayList<>();
    ArrayList<Operation> subList = new ArrayList<>();
    ArrayList<Operation> addSubList = new ArrayList<>();

    @Override
    public String toString() {
        return "OperationBase{" +
                "addList=" + addList +
                ", subList=" + subList +
                ", addSubList=" + addSubList +
                '}';
    }

    /*
    * 无参构造
    * */
    public OperationBase(){}

    /**
     * 使用IO流序列化批量存储OperationBase对象
     *
     * @param fileUrl 存储算式的文件路径
     * @param eq_num 存储算式数量
     * @param op 算数符号，+加，-减，2混合
     */
    public void setManyBinaryOperaiton(int eq_num,String fileUrl,char op) throws IOException {
        FileOutputStream fileOut = null;
        ObjectOutputStream out = null;
        try {
            fileOut = new FileOutputStream(fileUrl);
            out = new ObjectOutputStream(fileOut);
            this.setBinaryOperation(eq_num, op);     //存储算式BinaryOperation对象;
            out.writeObject(this);   //对象输出流，将ob对象写出到文件out
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
            fileOut.close();
        }
    }

    /**
     * 序列化准备：BinaryOperation对象中的集合ArrayList：算式判重
     * @param eq_num 算式数量
     * @param op 算数符号，+加，-减，2混合
     */
    public void setBinaryOperation(int eq_num,char op)  {
        ArrayList<Operation> list = this.createBinaryOperations(eq_num,op);  //生成算式
        if (op == '2'){
            this.addSubList = list;
        }else if (op== '+'){
            this.addList = list ;
        }else if (op=='-'){
            this.subList = list;
        }
    }

    /**
     * 生成指定数量算式
     *
     * @param op 指定算式符号，2混合，+加，-减
     * @param eq_num 要生成的算式数量
     * */
    public ArrayList<Operation> createBinaryOperations (int eq_num,char op) {
        ArrayList<Operation> list = new ArrayList<>();
        Operation e = null;
        for (int i = 0; i < eq_num; i++) {
            do {
                if (op == '2'){
                    e= new BinaryOperation();
                }else if (op == '+'){
                    e= new AddBinaryOperation();
                }else if (op == '-'){
                    e = new SubBinaryOperation();
                }
            } while (Operation.occursIn(e,list, i));  //判断是否算式是否相似或重复,若重复返回true
            list.add(e);
        }
        return list;
    }
    /**
     * 批量随机读取算式
     *
     * @param fileUrl  被读取算式的文件路径
     * @return 返回一个OperationBase对象，该对象成员属性有BinaryOperation集合类ArrayList
     * */
    public OperationBase getManyBinaryOperaiton(String fileUrl) throws IOException, ClassNotFoundException {
        OperationBase ob = new OperationBase();
        boolean fileIsExists = new File(fileUrl).exists();
        if (!fileIsExists){
            System.out.println("文件"+fileUrl+"不存在啊宝贝！先去生成算式吧");
        }else{
            FileInputStream fileIn = new FileInputStream(fileUrl);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            ob = (OperationBase) in.readObject();  //反序列化，从文件中读取对象
            in.close();
            fileIn.close();
        }
        return ob;

    }

    /**
     * 打印算式
     *
     * @param num_per_line  一行打印几个算式
     * @param exercise   传入一个集合【{}，{}。。。】
     * @param type  打印不含答案的算式or不含答案的算式，0不含,1含答案
     * */
    public static void formatAndDisplayExercise(ArrayList<Operation> exercise, int num_per_line,int type) throws IOException {
        int i, m, n, v;
        char o;
        Operation e;
        for(i = 0; i < exercise.size(); i++) {
            e = exercise.get(i);
            m = e.getLeft_operand();
            n = e.getRight_operand();
            o = e.getOperator();
            v = e.getValue();
            if(i % num_per_line == 0) {
                System.out.println();
                System.out.printf("%2d ~ %2d:    ", i+1, i+5);
            }
            if(o == '+') {  //加法算式,操作符：0--加法；1--减法
                if (type == 0){
                    System.out.printf("%2d + %2d =     ", m, n);
                }else if(type == 1){
                    System.out.printf(" %3d    ", v);
                }else{
                    System.out.printf("%2d + %2d =  %3d     ", m, n,v);
                }
            }else {
                if (type == 0){
                    System.out.printf("%2d - %2d =     ", m, n);
                }else if(type == 1){
                    System.out.printf(" %3d    ", v);
                }else{
                    System.out.printf("%2d - %2d =  %3d     ", m, n,v);
                }
            }
        }
    }


}
